<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

class Level_MxShopPage extends WebPage {

    function main() {
        global $_W, $_GPC;
        $set = m('common')->getSysset();
        $shopset = $set['shop'];
        $default = array(
            'id' => 'default',
            'levelname' => empty($set['shop']['levelname']) ? '普通等级' : $set['shop']['levelname'],
            'discount' => $set['shop']['leveldiscount'],
            'ordermoney' => 0,
            'ordercount' => 0,
            'usercount' => pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_user') . ' where levelid=0 limit 1')
        );

        $condition = " and uniacid=:uniacid";
        $params = array(':uniacid' => $_W['platformid']);

        if ($_GPC['status'] != '') {
            $condition.=' and status=' . intval($_GPC['status']);
        }

        if (!empty($_GPC['keyword'])) {
            $_GPC['keyword'] = trim($_GPC['keyword']);
            $condition.=' and ( levelname like :levelname)';
            $params[':levelname'] = "%{$_GPC['keyword']}%";
        }

        $others = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_user_level') . " WHERE 1 {$condition} ORDER BY level asc", $params);

        foreach ($others as &$row) {
            $row['usercount'] = pdo_fetchcolumn('select count(1) from ' . tablename('mx_shop_user') . ' where levelid=:levelid limit 1', array(':levelid' => $row['id']));
        }
        unset($row);

        if(empty($_GPC['keyword'])){
            $list = array_merge(array($default), $others);
        }else{
            $list = $others;
        }
        include $this->template();
    }

    function add() {
        $this->post();
    }

    function edit() {
        $this->post();
    }

    protected function post() {
        global $_W, $_GPC;

        $id = trim($_GPC['id']);
        $set = m('common')->getSysset();

        $setdata = pdo_fetch("select * from " . tablename('mx_shop_sysset') . ' where uniacid=:uniacid limit 1', array(':uniacid' => $_W['platformid']));

        if ($id == 'default') {
            $level = array(
                'id' => 'default',
                'levelname' => empty($set['shop']['levelname']) ? '普通等级' : $set['shop']['levelname'],
                'discount' => $set['shop']['leveldiscount'],
                'ordermoney' => 0,
                'ordercount' => 0
            );
        } else {
            $level = pdo_fetch("SELECT * FROM " . tablename('mx_shop_user_level') . " WHERE id=:id and uniacid=:uniacid limit 1", array(':uniacid' => $_W['platformid'], ':id' => intval($id)));

            if(!empty($level)){
                $goodsids = iunserializer($level['goodsids']);
                if (!empty($goodsids)) {
//                    $goods = pdo_fetchall("SELECT id,uniacid,title,thumb FROM ".tablename('mx_shop_goods')." WHERE uniacid=:uniacid AND id IN (".implode(',',$goodsids).")",array(':uniacid'=>$_W['platformid']));
                }
            }
        }


        if ($_W['ispost']) {
            $status = intval($_GPC['status']);
            $data = array(
                'uniacid' => $_W['platformid'],
                'level' => intval($_GPC['level']),
                'levelname' => trim($_GPC['levelname']),
                'ordercount' => intval($_GPC['ordercount']),
                'ordermoney' => $_GPC['ordermoney'],
                'discount' => trim($_GPC['discount']),
                'status' => $status
            );

//            $goodsids = iserializer($_GPC['goodsids']);
//            $buygoods = intval($_GPC['buygoods']);

            if (!empty($id)) {
                if ($id == 'default') {
                    $updatecontent = "<br/>等级名称: {$set['shop']['levelname']}->{$data['levelname']}"
                            . "<br/>折扣: {$set['shop']['leveldiscount']}->{$data['discount']}";
                    $set['shop']['levelname'] = $data['levelname'];
                    $set['shop']['leveldiscount'] = $data['discount'];
                    m('common')->updateSysset($set);
                    plog('user.level.edit', "修改会员默认等级" . $updatecontent);
                } else {
//                    $data['goodsids'] = $goodsids;
//                    $data['buygoods'] = $buygoods;
                    $updatecontent = "<br/>等级名称: {$level['levelname']}->{$data['levelname']}"
                            . "<br/>折扣: {$level['discount']}->{$data['discount']}";
                    pdo_update('mx_shop_user_level', $data, array('id' => $id, 'uniacid' => $_W['platformid']));
                    plog('user.level.edit', "修改会员等级 ID: {$id}" . $updatecontent);
                }
            } else {
//                $data['goodsids'] = $goodsids;
//                $data['buygoods'] = $buygoods;
                pdo_insert('mx_shop_user_level', $data);
                $id = pdo_insertid();
                plog('user.level.add', "添加会员等级 ID: {$id}");
            }
            show_json(1, array('url' => webUrl('user/level')));
        }
        $level_array = array();
        for ($i = 0; $i < 101; ++$i) {
            $level_array[$i] = $i;
        }
        include $this->template();
    }

    function delete() {
        global $_W, $_GPC;
        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }
        $items = pdo_fetchall("SELECT id,levelname FROM " . tablename('mx_shop_user_level') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);
        foreach ($items as $item) {

            pdo_delete('mx_shop_user_level', array('id' => $item['id']));
            plog('user.level.delete', "删除等级 ID: {$item['id']} 标题: {$item['levelname']} ");
        }
        show_json(1, array('url' => referer()));
    }

    function status() {
        global $_W, $_GPC;
        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }
        $items = pdo_fetchall("SELECT id,levelname FROM " . tablename('mx_shop_user_level') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);

        foreach ($items as $item) {
            pdo_update('mx_shop_user_level', array('status' => intval($_GPC['status'])), array('id' => $item['id']));
            plog('user.level.edit', "修改会员等级状态<br/>ID: {$item['id']}<br/>标题: {$item['levelname']}<br/>状态: " . $_GPC['status'] == 1 ? '启用' : '禁用');
        }
        show_json(1, array('url' => referer()));
    }

}
